// pages/seat/seat.js
const util = require('../../utils/util.js')
var api = require('../../config/api.js');
const { $Toast } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    visible: false,
    autoVisible: false,
    actions: [{
      name: '取消',
    }],
    temp:[
      [1, 2, 3, 4, 5, 6],
      [7, 8, 9, 10,11,12],
    ],
    reA: {
      rsId: null,
      isDis: false,
    },
    reB: {
      rsId: null,
      isDis: false,
    },
    reC: {
      rsId: null,
      isDis: false,
    },
    seatList: [],
    tmpSeat: null,
    room: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    console.log(Number.parseInt(1/3)*30)
    console.log(options)
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
      console.log(data)

      self.setData({
        room: data.data
      })
      self.updateSeat()
    })
  },
  updateSeat() {
    util.request(api.SeatList + '/' + this.data.room.id, {}, 'POST').then(res => {
      console.log(res)
      // this.list = res.data
      this.setData({
        seatList: res
      })

    }).catch(err => {
      console.log(err)
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //页面滚动执行方式
  onPageScroll(event) {
    this.setData({
      scrollTop: event.scrollTop
    })
  },
  handleOpen() {
    this.setData({
      visible: true
    });
  },
  autoReserveOpen() {
    this.setData({
      autoVisible: true
    });
  },
  handleCancel() {
    this.setData({
      visible: false,
      autoVisible: false
    });
  },

  handleClick({
    detail
  }) {
    const index = detail.index + 1;
    console.log(this.tmpSeat)

    this.setData({
      visible: false,
      autoVisible: false
    });
  },
  clickItem(option) {
    this.tmpSeat = option.currentTarget.dataset.item;
    console.log(this.tmpSeat )
    let timeList = this.tmpSeat.seatTimeDto;
    if(timeList != null){
      this.setData({
        reA: { rsId: null, isDis: false },
        reB: { rsId: null, isDis: false },
        reC: { rsId: null, isDis: false },
      });
      for (let i = 0; i < timeList.length; i++){
        console.log(timeList[i])
        if (timeList[i].timeInterval == 0 || this.tmpSeat.sjdA == 1){
          this.setData({
            reA: { rsId: timeList[i].id, isDis: true}
          });
        }
        if (timeList[i].timeInterval == 1 || this.tmpSeat.sjdB == 1) {
          this.setData({
            reB: { rsId: timeList[i].id, isDis: true }
          });
        }
        if (timeList[i].timeInterval == 2 || this.tmpSeat.sjdC == 1) {
          this.setData({
            reC: { rsId: timeList[i].id, isDis: true }
          });
        }
      }
    }
    this.setData({
      visible: true
    });

  },
  reserve(val) {
    console.log(val)

    util.request(api.Reserve, {
      seatId: this.tmpSeat.id,
      timeInterval: val.currentTarget.dataset.time_interval
    }, 'POST').then(res => {
      console.log(res)
      if (res.success){
        this.updateSeat()
        this.setData({
          visible: false,
          autoVisible: false
        });
      }
      
      $Toast({
        content: res.msg,
        type: 'success'
      });
      // this.list = res.data
    }).catch(err => {
      console.log(err)
    });
  },
  autoReserve(val) {
    console.log(this.data.room)

    util.request(api.AutoReserve, {
      roomId: this.data.room.id,
      timeInterval: val.currentTarget.dataset.time_interval
    }, 'POST').then(res => {
      console.log(res)
      $Toast({
        content: res.msg,
        type: 'success'
      });
      // this.list = res.data
    }).catch(err => {
      console.log(err)
    });
  },
  report(val) {
    let self = this

    console.log(val)
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths

        wx.uploadFile({
          url: api.Upload, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          header:{
            'userToken': wx.getStorageSync('userToken')
          },
          formData: {
            'seatId': self.tmpSeat.id,
            'timeInterval': val.currentTarget.dataset.time_interval
          },
          success(res) {
            const data = res.data
            $Toast({
              content: res.data,
              type: 'success'
            });
            console.log(res)
            //do something
          }
        })
      }
    })
  }
})
